package com.zzl.mine.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.zzl.mine.pojo.system.User;
import com.zzl.mine.util.Const;
import com.zzl.mine.util.Jurisdiction;

/**
 * @description 登录过滤，权限验证
 * @author rambo
 * @date 2018年6月7日 下午2:46:55
 */
public class LoginHandlerInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		String path = request.getServletPath();
		if (path.matches(Const.NO_INTERCEPTOR_PATH)) {
			return true;
		} else {
			User user = (User) Jurisdiction.getSession().getAttribute(Const.SESSION_USER);
			if (user != null) {
				path = path.substring(1, path.length());
				boolean b = Jurisdiction.hasJurisdiction(path);//访问权限校验
				if (!b) {
					response.sendRedirect(request.getContextPath() + Const.LOGIN);
				}
				return b;
			} else {
				response.sendRedirect(request.getContextPath() + Const.LOGIN);//登陆过滤
				return false;
			}
		}
	}

}
